using System;using System.Collections.Generic;using System.Collections.ObjectModel;using System.ComponentModel;using System.Linq;using System.Text;using System.Threading.Tasks;using System.Windows.Input;using Prism.Commands;using Prism.Mvvm;using MyToDo.Common;using System.Runtime.CompilerServices;

namespace MyToDo.ViewModels
{
    /// <summary>
    /// 用于演示MultiSelectComboBox控件的视图模型
    /// </summary>
    public class MultiSelectComboBoxDemoViewModel : BindableBase
    {
        #region 数据模型类

        /// <summary>
        /// 用于演示的通用数据项类
        /// </summary>
        public class Item
        {
            public int Id { get; set; }
            public string Name { get; set; }
            public override string ToString()
            { return Name; }
        }

        #endregion

        #region 私有字段

        private ObservableCollection<Item> fruits;               // 水果数据源
        private ObservableCollection<Item> selectedFruits;       // 选中的水果
        private ObservableCollection<Item> cities;               // 城市数据源
        private ObservableCollection<Item> selectedCities;       // 选中的城市
        private ObservableCollection<Item> languages;            // 语言数据源
        private ObservableCollection<Item> selectedLanguages;    // 选中的语言
        private int nextLanguageId = 6;     // 下一个语言ID

        #endregion

        #region 属性

        /// <summary>
        /// 水果数据源
        /// </summary>
        public ObservableCollection<Item> Fruits
        {
            get { return fruits; }
            set { SetProperty(ref fruits, value); }
        }

        /// <summary>
        /// 选中的水果
        /// </summary>
        public ObservableCollection<Item> SelectedFruits
        {
            get { return selectedFruits; }
            set { SetProperty(ref selectedFruits, value); }
        }

        /// <summary>
        /// 城市数据源
        /// </summary>
        public ObservableCollection<Item> Cities
        {
            get { return cities; }
            set { SetProperty(ref cities, value); }
        }

        /// <summary>
        /// 选中的城市
        /// </summary>
        public ObservableCollection<Item> SelectedCities
        {
            get { return selectedCities; }
            set { SetProperty(ref selectedCities, value); }
        }

        /// <summary>
        /// 编程语言数据源
        /// </summary>
        public ObservableCollection<Item> Languages
        {
            get { return languages; }
            set { SetProperty(ref languages, value); }
        }

        /// <summary>
        /// 选中的编程语言
        /// </summary>
        public ObservableCollection<Item> SelectedLanguages
        {
            get { return selectedLanguages; }
            set { SetProperty(ref selectedLanguages, value); }
        }

        #endregion

        #region 命令

        /// <summary>
        /// 添加新语言命令
        /// </summary>
        public ICommand AddLanguageCommand { get; private set; }

        /// <summary>
        /// 清除选择命令
        /// </summary>
        public ICommand ClearSelectionCommand { get; private set; }

        #endregion

        #region 构造函数

        /// <summary>
        /// 构造函数
        /// </summary>
        public MultiSelectComboBoxDemoViewModel()
        {
            // 初始化命令
            AddLanguageCommand = new DelegateCommand(AddNewLanguage);
            ClearSelectionCommand = new DelegateCommand(ClearAllSelections);

            // 初始化数据
            InitializeData();
        }

        #endregion

        #region 私有方法

        /// <summary>
        /// 初始化演示数据
        /// </summary>
        private void InitializeData()
        {
            // 初始化水果数据
            Fruits = new ObservableCollection<Item>
            {
                new Item { Id = 1, Name = "苹果" },
                new Item { Id = 2, Name = "香蕉" },
                new Item { Id = 3, Name = "橙子" },
                new Item { Id = 4, Name = "草莓" },
                new Item { Id = 5, Name = "葡萄" },
                new Item { Id = 6, Name = "西瓜" },
                new Item { Id = 7, Name = "猕猴桃" }
            };
            SelectedFruits = new ObservableCollection<Item>();

            // 初始化城市数据
            Cities = new ObservableCollection<Item>
            {
                new Item { Id = 1, Name = "北京" },
                new Item { Id = 2, Name = "上海" },
                new Item { Id = 3, Name = "广州" },
                new Item { Id = 4, Name = "深圳" },
                new Item { Id = 5, Name = "杭州" },
                new Item { Id = 6, Name = "南京" },
                new Item { Id = 7, Name = "武汉" },
                new Item { Id = 8, Name = "成都" }
            };
            SelectedCities = new ObservableCollection<Item>();

            // 初始化编程语言数据
            Languages = new ObservableCollection<Item>
            {
                new Item { Id = 1, Name = "C#" },
                new Item { Id = 2, Name = "Java" },
                new Item { Id = 3, Name = "Python" },
                new Item { Id = 4, Name = "JavaScript" },
                new Item { Id = 5, Name = "C++" }
            };
            SelectedLanguages = new ObservableCollection<Item>();
        }

        /// <summary>
        /// 添加新的编程语言
        /// </summary>
        private void AddNewLanguage()
        {
            if (Languages is ObservableCollection<Item> languagesCollection)
            {
                var newLanguage = new Item { Id = nextLanguageId++, Name = "新语言 " + (nextLanguageId - 1) };
                languagesCollection.Add(newLanguage);
            }
        }

        /// <summary>
        /// 清除所有选择
        /// </summary>
        private void ClearAllSelections()
        {
            SelectedFruits?.Clear();
            SelectedCities?.Clear();
            SelectedLanguages?.Clear();
        }

        #endregion
    }
}